<template>
<div class="ivu-card ivu-card-bordered ivu-card-dis-hover">
  <div class="ivu-card-head">
    <p>{{ $route.meta.title }}</p>
    <router-link :to="{name:'backendAccountCreate'}" class="ivu-btn ivu-btn-primary">创建会员</router-link>
  </div>

  <div class="ivu-card-body"><Table :columns="list.columns" :data="list.items" :loading="list.loading"></Table></div>

  <div class="ivu-card-body"><Page :current="pager.current" :total="pager.totalItems" :page-size="pager.limit" show-total @on-change="onChangePage"></Page></div>
</div>
</template>
<script>
import Vue from 'vue';
import { Table, Page, Poptip, Message } from 'iview';
import ajaxiView from '@/utils/ajaxiView';
import apiConfig from '@/utils/apiConfig';
import auth from '@/utils/auth';

Vue.component('Poptip', Poptip);

export default {
  components: {
    [Table.name]: Table,
    [Page.name]: Page
  },
  data() {
    return {
      apiConfig: {},
      token: auth.getToken(),
      list: {
        columns: [
          {title: 'ID', key: 'account_id', align: 'center'},
          {title: '用户名', key: 'account', align: 'center'},
          {title: '创建时间', key: 'create_time', align: 'center', render: (h, params) => { return h('span', new Date(parseInt(params.row.create_time) * 1000).toLocaleString()); }},
          {title: '状态', key: 'status', align: 'center', render: (h, params) => { return h('span', this.apiConfig['backstage_account_status'][params.row.status]); }},
          {title: '操作', key: 'action', align: 'center', render: (h, params) => {
            var _this = this;
            var opeare = [
              h('a', {
                class: 'ivu-btn ivu-btn-primary ivu-btn-small',
                on: {
                    click() {
                      _this.$router.push({name: 'backendAccountUpdate', params: {'id': params.row.account_id}});
                    }
                  }
              }, '修改'),
              h('Poptip', {
                props: {
                    confirm: true,
                    title: '删除后不可恢复?',
                    transfer: true
                },
                on: {
                  "on-ok": () => {
                    var _this = this;
                    ajaxiView.delete('/backstage/account/'+params.row.account_id+'?token='+this.token).then(function(data) {
                      if(data !== false) {
                        Message.success(data.msg);
                        _this.onChangePage(1);
                      }
                    });
                  }
                }
              }, [
                h('a', {
                  class: 'ivu-btn ivu-btn-error ivu-btn-small',
                }, '删除')
              ])
            ];

            return h('div', opeare);
          }}
        ],
        loading: false,
        items: []
      },
      pager: {
        current: 1,
        limit: 10,
        totalItems: 0
      }
    };
  },
  methods: {
    _initialize() {
      var _this = this;
      apiConfig.getItem().then(function(api_config) {
        _this.apiConfig = api_config;
        _this.getData();
      });
    },
    getData() {
      let _this = this;
      _this.list.loading = true;
      ajaxiView.get('/backstage/account?token='+_this.token+'&page='+_this.pager.current).then(function(data) {
        if(data !== false && typeof(data.pager) != 'undefined') {
          _this.list.items = data.pager.items;
          _this.pager.limit = data.pager.limit;
          _this.pager.totalItems = data.pager.total_items;

          _this.list.loading = false;
        }
      });
    },
    onChangePage(current) {
      this.pager.current = current;
      this.getData();
    }
  },
  mounted() {
    this._initialize();
  }
}
</script>
<style>
.ivu-table-cell .ivu-btn.ivu-btn-small {
  margin-right: 5px;
}
</style>